package com.fanrui.code.gate.core;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.fanrui.code.thread.NamedThreadFactory;

/**
 * 清理长期没有ReqHeatBeat的Session
 *
 */
public class SessionCleaner {
	private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1,
			new NamedThreadFactory("SessionCleaner-"));

	/**
	 * 每隔delayTime（毫秒）时间，查找所有Session，关闭连续timeoutTime（毫秒）时间内没有接收到任何请求的Session
	 * 
	 * @param timeoutTime
	 * @param delayTime
	 */
	public static void start(int timeoutTime, int delayTime) {
		executor.scheduleWithFixedDelay(new Runnable() {

			@Override
			public void run() {
				GateServerHandler.getInstance().cleanTimeoutedSession(timeoutTime);
			}

		}, delayTime, delayTime, TimeUnit.MILLISECONDS);
	}
}

